// stores/user.js
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  const userInfo = ref(null)
  
  // 获取用户信息（包含job字段）
  const setUserInfo = (info) => {
    userInfo.value = info
    // 同时存储到本地，避免刷新丢失
    uni.setStorageSync('userInfo', info)
  } 
  
  // 从本地存储初始化
  const initUserInfo = () => {
    const localInfo = uni.getStorageSync('userInfo')
    if (localInfo) {
      userInfo.value = localInfo
    }
  }
  
  // 获取job字段的便捷方法
  const getJob = () => {
    return userInfo.value?.job || ''
  }

  
  // 清空用户信息
  const clearUserInfo = () => {
    userInfo.value = null
    uni.removeStorageSync('userInfo')
    uni.removeStorageSync('token')
  }
  
  return {
    userInfo,
    setUserInfo,
    initUserInfo,
    getJob,
    clearUserInfo
  }
})